package com.situ.springboot.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.situ.springboot.mapper.BlogMapper;
import com.situ.springboot.pojo.Blog;
import com.situ.springboot.pojo.query.BlogQuery;
import com.situ.springboot.service.IBlogService;
import com.situ.springboot.util.JSONResult;
import com.situ.springboot.util.LayUITableJSONResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class BlogServiceImpl implements IBlogService {
    @Autowired
    private BlogMapper blogMapper;

    @Override
    public LayUITableJSONResult selectByPage(BlogQuery blogQuery) {
        PageHelper.startPage(blogQuery.getPage(), blogQuery.getLimit());
        //int offset = (blogQuery.getPage() - 1) * blogQuery.getLimit();
        //blogQuery.setOffset(offset);
        List<Blog> list = blogMapper.selectByPage(blogQuery);
        //int totalCount = blogMapper.selectTotalCount(blogQuery);
        PageInfo pageInfo = new PageInfo(list);
        long totalCount = pageInfo.getTotal();
        return LayUITableJSONResult.ok((int)totalCount, list);
    }

    @Override
    public JSONResult deleteById(Integer id) {
        int count = blogMapper.deleteById(id);
        return count == 1 ? JSONResult.ok("删除成功") : JSONResult.error("删除失败");
    }

    @Override
    public JSONResult add(Blog blog) {
        int count = blogMapper.add(blog);
        return count == 1 ? JSONResult.ok("添加成功") : JSONResult.error("添加失败");
    }

    @Override
    public Blog selectById(Integer id) {
        return blogMapper.selectById(id);
    }

    @Override
    public JSONResult update(Blog blog) {
        int count = blogMapper.update(blog);
        return count == 1 ? JSONResult.ok("更新成功") : JSONResult.error("更新失败");
    }

    @Override
    public JSONResult deleteAll(Integer[] ids) {
        int count = blogMapper.deleteAll(ids);
        return count == ids.length ? JSONResult.ok("删除成功") : JSONResult.error("删除失败");
    }
}
